﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using Ivi.Visa;
using Ivi.Visa.FormattedIO;
using NationalInstruments.Visa;
using ResourceManager = NationalInstruments.Visa.ResourceManager;

namespace TempHum.service
{
    class visaUtil
    {
        public static ResourceManager manager = new ResourceManager();
        public static IMessageBasedSession opensession(string resource) {
           // manager.Dispose();
           return (MessageBasedSession)manager.Open(resource);
        }
        public static string readAndconfig(IMessageBasedSession mbSession,string config)
        {
         //   Console.WriteLine("CONF:" + config + "\n");
            mbSession.RawIO.Write("CONF:"+ config+"\n");
            mbSession.RawIO.Write("READ?\n");
            return mbSession.RawIO.ReadString();
        }
        public static void closesession(IMessageBasedSession mbSession)
        {
            if (mbSession != null) {
                mbSession.Dispose();
            }          
        }
        public static string TestVisa(IMessageBasedSession mbSessio) {
            //try
            //{

            //    if (mbSession == null) {
            //        var rmSession = new ResourceManager();
            //        mbSession = (MessageBasedSession)rmSession.Open("USB0::0x2A8D::0x0201::MY57700503::INSTR");
            //          mbSession.RawIO.Write("CONF:VOLT:DC 10");
            //        // mbSession.RawIO.Write("TRIG:SOUR EXT");
            //    }


            //    mbSession.RawIO.Write("READ?");

            //    //Console.WriteLine(mbSession.RawIO.ReadString());
            //    return mbSession.RawIO.ReadString();
            //}
            //catch (InvalidCastException e) {


            //}
            mbSessio.RawIO.Write("READ?\n");
            return mbSessio.RawIO.ReadString();
        }
   
    }
}
